package com.zhijian.warehouse.manage.controller;


import com.zhijian.response.annotation.Response;
import com.zhijian.util.BeanUtil;
import com.zhijian.util.JsonUtil;
import com.zhijian.warehouse.manage.service.ItsmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统维护
 */
@Slf4j
@Controller
@Api(tags = "系统维护升级")
public class ItsmController {
	@Autowired
    private ItsmService service;

    /**
     * 系统升级
     *
     * @param file
     * @param sk 密钥
     * @return
     */
    @Response
    @PostMapping("/ssom/upge")
    @ApiOperation("系统升级")
    public boolean upgrade(@RequestParam(value = "sk") String sk,
            @RequestParam(name = "file") MultipartFile file) {
        log.info("{} - {}| upge info", this.getClass().getSimpleName(), sk);
        return service.upgrade(file, sk);
    }

    /**
     * apk升级
     *
     * @param file
     * @param sk 密钥
     * @return
     */
    @Response
    @PostMapping("/ssom/upge/apk")
    @ApiOperation("apk上传")
    public boolean upApk(@RequestParam(value = "sk") String sk,
            @RequestParam(name = "file") MultipartFile file) {
        log.info("{} - {}| upApk info", this.getClass().getSimpleName(), sk);
        return service.upApk(file, sk);
    }


    /**
     * 日志查询
     *
     * @param date yyyy-MM-dd
     * @return
     */
    @Response
    @PostMapping("/ssom/log")
    public List<Map<String, Object>> selectLogs(@RequestBody Map<String, Object> paramMap) {
        if (paramMap == null)
            paramMap = new HashMap<String, Object>();
        log.info("{} - {}| query logs", this.getClass().getSimpleName(), paramMap);
        return service.selectLogs(BeanUtil.getStr4Map("startTime", paramMap, null));
    }

    /**
     * 日志下载
     *
     * @param paramMap
     * @return
     */
    @GetMapping("/ssom/log/download/{fileName}")
    public void downloadLog(@PathVariable("fileName") String fileName, HttpServletResponse response) {
        log.info("{} - {}| download logs", this.getClass().getSimpleName(), fileName);
        service.downloadLog(fileName, response);
    }
}
